package cn.iocoder.yudao.framework.common.util.json;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonValidator {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static boolean isValidJson(String jsonString) {
        try {
            objectMapper.readTree(jsonString);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) {
        String jsonStringArray = "[\"apple\", \"banana\", \"cherry\"]";
        System.out.println(isValidJson(jsonStringArray));  // 输出 true

        String jsonStringObject = "{\"name\":\"John\", \"age\":30}";
        System.out.println(isValidJson(jsonStringObject));  // 输出 true

        System.out.println(isValidJson("abc"));  // 输出 false

    }
}
